/**
 *  @file SDLKeyboardController.cpp
 *
 *  @date 16-nov-2009
 *  @author Ives van der Flaas
 */

#include "SDLKeyboardController.h"
#include <SDL.h>

si_sdl::SDLKeyboardController::SDLKeyboardController(si::Vector2 position, si::Vector2 size):
	Controller(position,size),
	fGoingLeft(false),
	fGoingRight(false)
{

}

void si_sdl::SDLKeyboardController::update(double time)
{
	Uint8 *keystates = SDL_GetKeyState(0);

	const double speed = 3;

	if(keystates[SDLK_LEFT] && keystates[SDLK_RIGHT])
		return;

	if (keystates[SDLK_LEFT])
	{
		if(fPosition.getX() >= -4)
		{
			fPosition.setX(fPosition.getX() - time*speed);
			fGoingLeft = true;
		}
		else
			fGoingLeft = false;
	}
	else
		fGoingLeft = false;

	if (keystates[SDLK_RIGHT])
	{
			if(fPosition.getX() <= 4-fSize.getX())
			{
				fPosition.setX(fPosition.getX() + time*speed);
				fGoingRight = true;
			}
			else
				fGoingRight = false;
	}
	else
		fGoingRight = false;


	if (fPosition.getX() >= 4-fSize.getX())
		fPosition.setX(4-fSize.getX());
	if (fPosition.getX() <= -4.0)
		fPosition.setX(-4.0);
}


